/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>Project: javaseapp - T4
 * <p>Powered by webrx On 2021-08-26 18:05:31
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class T4 {
    AtomicInteger num = new AtomicInteger(0);
    ReentrantLock lock = new ReentrantLock(true);

    void work() {
        String tn = Thread.currentThread().getName();
        while (true) {
            lock.lock();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.printf("%s%d  ", tn, num.incrementAndGet());
            if ("C".equals(tn)) {
                System.out.println();
            }
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        var t = new T4();
        var a = new Thread(t::work, "A");
        a.start();

        var b = new Thread(t::work, "B");
        b.start();

        var c = new Thread(t::work, "C");
        c.start();

    }
}
